
Page({

  /**
   * 组件的初始数据
   */
  data: {
    bg: [],//轮播图
    current: 0,//轮播页数
    num: 1,//数量
    price: 0,//单价
    token: '',
    good_id: 0,//商品ID
    money: 0,//总价
    good_pic: "",//商品信息的图片
    show: false,//弹窗显示
    car_num: 0,//购物车里面的商品数量
    shop_car_num: true,
    shop_car_num2:false,//当前商品是否在购物车
    car_num_this:0,//当前商品在购物车的数量
    good_name:'',//商品名字
    sku:[],//样式
    info_img:[],//商品详情图片
    spe:'',//传过去的sku
    Choose_btn:false,
    first_id:0,
    goods_info:'',
    data_img:"",
    com_not:false,
    com_have:false,

  },
  onLoad: function (options) {
    let that = this
    let arr = []
    console.log(options.indexArr)
    that.data.good_id = options.indexArr
    that.setData({
      good_id:options.indexArr
    })
    // console.log(that.data.good_id)
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#2BBDCA',
      animation: {
        duration: 400,
        timingFunc: 'easeInOut'
      }
    })
    if (that.data.num == 1) {
      that.setData({
        btn: true
      })
    } else if (that.data.num != 1) {
      that.setData({
        btn: false
      })
    }
    console.log(that.data.btn)
    // 请求商品详情
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/goodInfo',
      method: "POST",
      data: {
        good_id: that.data.good_id,
      }, success: function (res) {
        console.log(res)
        arr = (res.data.data.info[0])
        that.data.bg = JSON.parse(arr.imgs)//图片轮播
        that.data.good_id = res.data.data.good_id//商品id
        that.data.price = res.data.data.price//商品单价
        that.data.good_name = res.data.data.good_name
        that.data.money = (that.data.num) * (that.data.price) //商品总价
        that.data.sku = JSON.parse(res.data.data.info[0].edition)//sku
        that.data.info_img = JSON.parse(res.data.data.info[0].imgs)//商品详情图片
        that.data.goods_info = that.data.sku[0].goods_info
        that.data.price = that.data.sku[0].goods_price
        that.data.data_img = that.data.sku[0].goods_img
        that.setData({
          bg: that.data.bg,
          goood_id: res.data.data.good_id,
          price: res.data.data.price,
          money: (that.data.num) * (that.data.price),
          good_name : res.data.data.good_name,
          sku:JSON.parse(res.data.data.info[0].edition),
          info_img : JSON.parse(res.data.data.info[0].imgs),
          goods_info: that.data.sku[0].goods_info,
          price: that.data.sku[0].goods_price,
          data_img: that.data.sku[0].goods_img
        })
        console.log(that.data.sku)
      }

    })
    // 获取缓存的token
    wx.getStorage({
      key: 'token',
      success(res) {
        console.log(res.data)
        that.data.token = res.data
        that.setData({
          token: res.data
        })
        // 请求购物车列表
        wx.request({
          url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList',
          method: "post",
          data: {
            token: that.data.token
          }, success: function (res) {
            console.log(res)
            if (res.data.data.data.length == 0) {
              that.data.shop_car_num = false
              that.setData({
                shop_car_num: false
              })
            } else {
              let j = 0
              for (let i = 0; i < res.data.data.data.length; i++) {
                j += res.data.data.data[i].num
              }
              that.setData({
                car_num: j
              })
            }
            for(let i = 0; i <res.data.data.data.length; i++){
              if(res.data.data.data[i].good_id == that.data.good_id){
                that.data.car_num_this =  res.data.data.data[i].num
                that.data.shop_car_num2 = true
                console.log("yes")
                that.setData({
                  car_num_this : res.data.data.data[i].num,
                  shop_car_num2 :true
                })
                break;
              }else{
                that.data.shop_car_num2 = false
                console.log("no")
                that.setData({
                  shop_car_num2 : false
                })
              }
            }
          }
        })
        wx.request({
          url: 'http://api_devs.wanxikeji.cn/api/commentList',
          method:"post",
          data:{
            token : that.data.token,
            good_id : that.data.good_id
          },success(res){
            console.log(res)
            if(res.data.data.list.length == 0){
              that.data.com_not = true,
              that.data.com_have = false
              that.setData({
                com_not: true,
                com_have: false
              })
            }else {
              that.data.com_not = false,
              that.data.com_have = true
              that.setData({
                com_not:false ,
                com_have: true
              })
            }
          }
        })
      }
    })

  },

    // 轮播函数
    swiperChange: function (e) {
      var that = this;
      that.setData({
        current: e.detail.current
      })
    },
    //加减函数
    add() {
      let that = this;
      that.data.num++;
      that.data.money = (that.data.num) * (that.data.price)
      that.setData({
        num: that.data.num++
      })
      console.log(that.data.money)
    },
    sub() {
      let that = this;
      if (that.data.num == 1) return
      that.data.money = ((that.data.num) - 1) * (that.data.price)
      that.data.num--;
      that.setData({
        num: that.data.num--
      })
    },
    // 显示购物车的弹窗
    show() {
      this.setData({
        show: true
      })
    },
    // 关闭弹窗
    close() {
      this.setData({
        show: false
      })
    },
    // 加入购物车
    submit() {
      let that = this
      if(that.data.token == ""){
        wx.showToast({
          title: '请先登录！',
          icon: 'error',
          duration: 2000
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/user/user',
          })
        },2000)
      }else {
        wx.showToast({
          title: '加入购物车成功',
          icon: 'success',
          duration: 2000
        })
      }

      wx.request({
        url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList',
        method: "POST",
        data: {
          token: that.data.token,
        }, success: function (res) {
          console.log(res)
          if (res.data.data.data.length == 0) {
            that.data.shop_car_num = false
            that.setData({
              shop_car_num: false
            })
          } else {
            that.setData({
              shop_car_num: true
            })
          }
          let i = res.data.data.data.findIndex(item => item.good_id === that.data.good_id)
          if (i == -1) {
            console.log("没有")
            wx.request({
              url: 'http://api_devs.wanxikeji.cn/api/shoppingCarAddModify',
              method: "POST",
              data: {
                token: that.data.token,
                good_id: that.data.good_id,
                num: that.data.num,
                price: Number(that.data.price),
                money: that.data.money,
                sku: that.data.goods_info,
                // shopping_car_id:res.data.data.data[i].shopping_car_id
              }, success: function (res) {
                console.log(res)
                let j = that.data.car_num//创建一个变量来接
                that.data.car_num = (that.data.num) + j
                that.setData({
                  car_num: (that.data.num) + j,
                  shop_car_num: true
                })
                console.log(that.data.car_num)
                wx.request({
                  url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList',
                  method: "post",
                  data: {
                    token: that.data.token
                  }, success: function (res) {
                    console.log(res)
                    if (res.data.data.data.length == 0) {
                      that.data.shop_car_num = false
                      that.setData({
                        shop_car_num: false
                      })
                    } else {
                      let j = 0
                      for (let i = 0; i < res.data.data.data.length; i++) {
                        j += res.data.data.data[i].num
                      }
                      that.setData({
                        car_num: j
                      })
                    }
                    for(let i = 0; i <res.data.data.data.length; i++){
                      if(res.data.data.data[i].good_id == that.data.good_id){
                        that.data.car_num_this =  res.data.data.data[i].num
                        that.data.shop_car_num2 = true
                        console.log("yes")
                        that.setData({
                          car_num_this : res.data.data.data[i].num,
                          shop_car_num2 :true
                        })
                        break;
                      }else if(res.data.data.data[i].good_id != that.data.good_id) {
                        that.data.shop_car_num2 = false
                        console.log("no")
                        that.setData({
                          shop_car_num2 : false
                        })
                      }
                    }
                  }
                })
              }
            })
          } else {
            console.log(res)
            console.log("有")
            wx.request({
              url: 'http://api_devs.wanxikeji.cn/api/shoppingCarAddModify',
              method: "POST",
              data: {
                token: that.data.token,
                good_id: that.data.good_id,
                num: (that.data.num) + (res.data.data.data[i].num),
                price: Number(that.data.price),
                money: that.data.money,
                sku: that.data.goods_info,
                shopping_car_id: res.data.data.data[i].shopping_car_id
              }, success: function (res) {
                console.log(res)
                let j = that.data.car_num
                that.data.car_num = (that.data.num) + j
                that.setData({
                  car_num: (that.data.num) + j,
                  shop_car_num: true
                })
                console.log(that.data.car_num)
                that.data.ask = true
                console.log(that.data.ask)
                wx.request({
                  url: 'http://api_devs.wanxikeji.cn/api/shoppingCarList',
                  method: "post",
                  data: {
                    token: that.data.token
                  }, success: function (res) {
                    console.log(res)
                    if (res.data.data.data.length == 0) {
                      that.data.shop_car_num = false
                      that.setData({
                        shop_car_num: false
                      })
                    } else {
                      let j = 0
                      for (let i = 0; i < res.data.data.data.length; i++) {
                        j += res.data.data.data[i].num
                      }
                      that.setData({
                        car_num: j
                      })
                    }
                    for(let i = 0; i <res.data.data.data.length; i++){
                      if(res.data.data.data[i].good_id == that.data.good_id){
                        that.data.car_num_this =  res.data.data.data[i].num
                        that.data.shop_car_num2 = true
                        console.log("yes")
                        that.setData({
                          car_num_this : res.data.data.data[i].num,
                          shop_car_num2 :true
                        })
                        break;
                      }else if(res.data.data.data[i].good_id != that.data.good_id) {
                        that.data.shop_car_num2 = false
                        console.log("no")
                        that.setData({
                          shop_car_num2 : false
                        })
                      }
                    }
                  }
                })
              }
            })
          }
          // wx.request({
          //   url: 'http://api_devs.wanxikeji.cn/api/shoppingCarAddModify',
          //   method:"POST",
          //   data:{
          //     token: that.data.token,
          //     good_id: that.data.good_id,
          //     num:(that.data.num)+(res.data.data.data[i].num),
          //     price:Number(that.data.price),
          //     money:that.data.money,
          //     sku:"1",
          //     shopping_car_id:res.data.data.data[i].shopping_car_id
          //   },success:function(res){
          //     console.log(res)
          //     that.data.ask = true
          //     console.log(that.data.ask)
          //   }
          // })
        }


      })
    },
    // 跳转路由
    shop_page() {
      wx.switchTab({
        url: '/pages/shopingcar/shopingcar',
      })
    },
    to_comment(){
      let that = this
      wx.redirectTo({
          url:"/pages/comment/comment?indexArr="+ that.data.good_id
      })
    },
    // 点击传入sku
    choose_btn(e){
      let that = this
      console.log(e.currentTarget.dataset.index);
      var index = e.currentTarget.dataset.index
      // console.log(e.target.dataset.index)
      that.data.goods_info = that.data.sku[index].goods_info
      that.data.price = that.data.sku[index].goods_price
      that.data.data_img = that.data.sku[index].goods_img
      that.setData({
        first_id: index,
        goods_info: that.data.sku[index].goods_info,
        price: that.data.sku[index].goods_price,
        data_img: that.data.sku[index].goods_img
      })

      that.data.sku[index].name = that.data.good_id.name
      
      console.log(that.data.sku[index])

    },
    // buy(){
    //   wx.request({
    //     url: 'http://api_devs.wanxikeji.cn/api/shoppingCarAddModify',
    //     method:"post",
    //     data:{
    //       token: that.data.token,
    //       good_id: that.data.good_id,
    //       num: that.data.num,
    //       price: Number(that.data.price),
    //       money: that.data.money,
    //       sku: that.data.goods_info,
    //     },success(res){
    //       console.log(res)
    //     }
    //   })
    // }
})
